
package com.hlkj.pay.service.ad.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.common.constants.RedisConstants;
import com.hlkj.pay.dto.ad.query.AdStatisticsQueryDto;
import com.hlkj.pay.infrastructure.mapper.ad.AdStatisticsMapper;
import com.hlkj.pay.infrastructure.model.ad.AdStatisticsDO;
import com.hlkj.pay.service.ad.IAdStatisticsService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/23 9:06
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdStatisticsServiceImpl implements IAdStatisticsService {

    private final AdStatisticsMapper adStatisticsMapper;

    @Override
    public PageResult<AdStatisticsDO> queryPage(AdStatisticsQueryDto adStatisticsQueryDto) {
        // return adInfoConfigMapper.selectPage(adStatisticsQueryDto,
        // buildWrapper(adStatisticsQueryDto));
        adStatisticsQueryDto.setSortingFields("statistics.start_time");
        IPage<AdStatisticsDO> mpPage = MyBatisUtils.buildPage(adStatisticsQueryDto);
        QueryWrapper<AdStatisticsDO> queryWrapper = buildPageWrapper(adStatisticsQueryDto);
        adStatisticsMapper.selectStatisticsPage(mpPage, queryWrapper);
        
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }

    @Override
    public List<AdStatisticsDO> queryList(Long adId, Set<String> timeKeys) {
        List<AdStatisticsDO> adStatisticsList=new ArrayList<>();
        timeKeys.stream().forEach(timeKey->{
            AdStatisticsDO statisticsDO = queryOne(adId, timeKey);
            if(statisticsDO!=null){
                adStatisticsList.add(statisticsDO);
            }
        });
        return adStatisticsList;
    }
    @Override
    @CachePut(value = RedisConstants.AD_STATISTICS_TIME_KEY, key = "#adId+'_'+#timeKey", unless = "#result == null")
    public AdStatisticsDO queryOne(Long adId, String timeKey) {
        AdStatisticsQueryDto adStatisticsQueryDto = new AdStatisticsQueryDto();
        adStatisticsQueryDto.setAdId(adId);
        adStatisticsQueryDto.setTimeKey(timeKey);
        return adStatisticsMapper.selectOne(buildWrapper(adStatisticsQueryDto));
    }
    
    @Override
    public void updateAdStatistics(AdStatisticsDO adStatisticsDO) {
        adStatisticsMapper.updateAdStatistics(adStatisticsDO);
    }

    @Override
    @CachePut(value = RedisConstants.AD_STATISTICS_TIME_KEY, key = "#adStatisticsDO.adId+'_'+#adStatisticsDO.timeKey")
    public AdStatisticsDO addAdStatistics(AdStatisticsDO adStatisticsDO) {
        adStatisticsMapper.insert(adStatisticsDO);
        return adStatisticsDO;
    }

    /**
     * 查询条件封装
     *
     * @param adMediaQueryDO
     * @return
     */
    QueryWrapper<AdStatisticsDO> buildPageWrapper(AdStatisticsQueryDto adMediaQueryDO) {
        QueryWrapperX<AdStatisticsDO> queryWrapper = new QueryWrapperX<>();
        queryWrapper.likeIfPresent("info.name", adMediaQueryDO.getName());
        queryWrapper.eqIfPresent( "info.media_id", adMediaQueryDO.getMediaId());
        queryWrapper.eqIfPresent( "info.type", adMediaQueryDO.getAdType());
        queryWrapper.eqIfPresent( "statistics.type", adMediaQueryDO.getType());
        queryWrapper.geIfPresent( "statistics.start_time", adMediaQueryDO.getStartQueryTime());
        queryWrapper.leIfPresent("statistics.end_time", adMediaQueryDO.getEndQueryTime());
        return queryWrapper;
    }

    /**
     * 查询条件封装
     *
     * @param adStatisticsQueryDto
     * @return
     */
    LambdaQueryWrapper<AdStatisticsDO> buildWrapper(AdStatisticsQueryDto adStatisticsQueryDto) {
        LambdaQueryWrapperX<AdStatisticsDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(AdStatisticsDO::getAdId, adStatisticsQueryDto.getAdId());
        queryWrapper.inIfPresent(AdStatisticsDO::getTimeKey, adStatisticsQueryDto.getTimeKeys());
        queryWrapper.eq(AdStatisticsDO::getTimeKey, adStatisticsQueryDto.getTimeKey());
        return queryWrapper;
    }
}
